/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filters;

import beans.SessionBean;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Wizz
 */
public class LoginFilter implements Filter {

    private SessionBean sessionBean;

    public LoginFilter() {
    } 

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
	throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        
        String path = req.getRequestURI();

        if(req.getSession().getAttribute("sessionBean") == null) {
            sessionBean = new SessionBean();
            req.getSession().setAttribute("sessionBean", sessionBean);
        }

        if(!path.equals("/bookSelector/index.jspx") && sessionBean.getCurrentUser() == null){
            res.sendRedirect("index.jspx");
        }
        else
            chain.doFilter(request, response);


    }
    
   
    /**
     * Destroy method for this filter 
     */
    public void destroy() { 
    }

    /**
     * Init method for this filter 
     */
    public void init(FilterConfig filterConfig) { 

    }

   

}
